package edu.dayu.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import edu.dayu.mapper.BlogMapper;
import edu.dayu.mapper.TagMapper;
import edu.dayu.pojo.Paging;
import edu.dayu.pojo.Tag;
import edu.dayu.service.TagService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class TagServiceImpl implements TagService {
    @Autowired
    private TagMapper tagMapper;

    @Autowired
    private BlogMapper blogMapper;

    @Override
    public PageInfo<Tag> listTag(Paging paging) {
        if (paging.getPageNum()<1){
            paging.setPageNum(1);
        }
        PageHelper.startPage(paging.getPageNum(),paging.getPageSize());
        List<Tag> tagList = tagMapper.listTag();
        PageInfo<Tag> pageInfo = new PageInfo<>(tagList);
        return pageInfo;
    }

    @Override
    public List<Tag> listTag() {
        return tagMapper.listTag();
    }

    @Override
    public Tag getTagName(Tag tag) {
        return tagMapper.getTagName(tag);
    }

    @Override
    public Boolean saveTag(Tag tag) {
        Tag tagName = tagMapper.getTagName(tag);
        if (tagName==null)
            return tagMapper.saveTag(tag);
        else {
            return false;
        }
    }

    @Override
    public Boolean updateTag(Tag tag) {
        Tag tagName = tagMapper.getTagName(tag);
        if (tagName==null)
            return tagMapper.updateTag(tag);
        else {
            return false;
        }
    }

    @Override
    public void deleteTag(int id) {
        tagMapper.deleteTag(id);
    }

    @Override
    public List<Tag> countTagTopThree() {
        List<Tag> tags = tagMapper.listTag();
        int[] ints = blogMapper.listBlogPublished();
        List<int[]> intlist= new ArrayList<>();
        for (int i : ints) {
            intlist.add(blogMapper.listTagBlog(i));
        }
        int a [] = new int[100];
        int t=0;
        for (Tag tag : tags) {
            Integer integer = tagMapper.countTagTopThree(tag.getId());
            for (int[] ints1 : intlist) {
                for (int i : ints1) {
                    if (tag.getId()==i)
                        integer--;
                }
            }
            a[t++]=integer!=null?integer:0;
        }
        int out [] = new int[3];
        int input[] = new int [3];
        for (int i= 0 ;i<3;i++){
            int k =0;
            for (int j = 0 ;j < t ; j++){
                if (k<a[j]){
                    k=a[j];
                    out[i]=j;
                }
            }
            input[i]=a[out[i]];
            a[out[i]]=0;
        }
        if (out[0]==0)
            out[0]=1;
        for (int i =0 ; i<out.length && out[i]>0 ;i++){
            if (out[0]==1 && i==0)
                out[i]=tags.get(out[i]-1).getId();
            else
                out[i]=tags.get(out[i]).getId();
        }
        List<Tag> tags1 = tagMapper.listTagById(out);
        int i =0;
        for (Tag tag : tags1) {
            tags1.get(i).setCount(input[i]);
            i++;
        }
        return tags1;
    }

    @Override
    public Integer countTagAll() {
        return tagMapper.countTagAll();
    }

    @Override
    public List<Tag> countTagTopAll() {
        List<Tag> tags = tagMapper.listTag();
        int[] ints = blogMapper.listBlogPublished();
        List<int[]> intlist= new ArrayList<>();
        for (int i : ints) {
            intlist.add(blogMapper.listTagBlog(i));
        }
        int a [] = new int[100];
        int t=0;
        for (Tag tag : tags) {
            Integer integer = tagMapper.countTagTopThree(tag.getId());
            for (int[] ints1 : intlist) {
                for (int i : ints1) {
                    if (tag.getId()==i)
                        integer--;
                }
            }
            a[t++]=integer!=null?integer:0;
        }
        int[] out  = new int[100];
        int[] input = new int [100];
        for (int i= 0 ;i<t;i++){
            int k =-1;
            for (int j = 0 ;j < t ; j++){
                if (k<a[j]){
                    k=a[j];
                    out[i]=j;
                }
            }
            input[i]=a[out[i]];
            a[out[i]]=-1;
        }

        for (int i =0 ; i<t;i++){
            out[i]=tags.get(out[i]).getId();
        }
        List<Tag> tags1 = tagMapper.listTagById(out);
        int i =0;
        for (Tag tag : tags1) {
            tags1.get(i).setCount(input[i]);
            i++;
        }
        return tags1;
    }
}
